package casa.depesos.app.com.adapter;

import android.view.View;


import java.util.List;

import casa.depesos.app.com.dlg.Choose2Dlg;

public class Choose2Adapter extends ChooseAdapter {

    Choose2Dlg dlg;
    int level = 0;

    public Choose2Adapter(List list, Choose2Dlg dlg) {
        super(list, -1);
        this.dlg = dlg;
    }

    @Override
    public void setList(List list, int now) {
        level = 1;
        super.setList(list, now);
    }

    public void resetParent(List list, int now) {
        level = 0;
        super.setList(list, now);
    }

    @Override
    protected void bindHolder(Holder holder, int p) {
        holder.binding.title.setText(list.get(p).toString());
        holder.binding.img.setVisibility(level == 1 && p == current ? View.VISIBLE : View.INVISIBLE);
        holder.itemView.setOnClickListener(view -> {
            int tmp = holder.getBindingAdapterPosition();
            if (level == 0) {
                dlg.onProvinceChange(tmp, -1);
            } else {
                if (current == tmp) {
                    return;
                }
                dlg.onCityChange(list.get(tmp).toString(), tmp);
                current = tmp;
                notifyDataSetChanged();
            }
        });
    }

    public String getCurrentValue() {
        try {
            return list.get(current).toString();
        } catch (Throwable e) {
            e.printStackTrace();
            return "";
        }
    }
}
